<?php
namespace Api\Controller;

use Common\Controller\AdminbaseController;

class GuestbookadminController extends AdminbaseController{
	
	protected $guestbook_model;
	
	public function _initialize() {
		parent::_initialize();
		$this->guestbook_model=D("Common/Guestbook");
	}

	// 后台留言列表
	public function index(){

		$status  = isset($_POST['status']) ? intval($_POST['status']):-1;


		if($status===1 || $status===2 ){
			if($status==1) {
				$where = array(
					'status' => 0,
				);
			}else{
				$where = array(
					'status' => 1,
				);
			}
		}else{
			$where = '';
		}
		$db = M('userinfo');


//		$guestmsgs=$db->where(array("status"=>1))->order(array("add_time"=>"DESC"))->limit($page->firstRow . ',' . $page->listRows)->select();
//		$this->assign("page", $page->show('Admin'));
//		$page = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
//		$pageSize = $_REQUEST['pageSize'] ? $_REQUEST['pageSize'] : 3;
//
//
//		$res = new \Think\Page($count, $pageSize);
//
//		$guestmsgs =D('Guestbook')->getMenus($page,$pageSize);
//		$this->assign("page", $res->show());
//		$this->assign("guestmsgs",$list);
//
		$count = $db->where($where)->count();// 查询满足要求的总记录数
		$Page  = $this->Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show  = $Page->show("Admin");// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list  = $db->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$this->assign("guestmsgs",$list);
		$this->assign('page',$show);// 赋值分页输出

		$this->assign("status",$status);

		$this->display();
	}


	// 删除留言
	public function delete(){
		$db = M('userinfo');
		$id=I("get.id",0,'intval');
		$result=$db->where(array("id"=>$id))->delete();
		if($result!==false){
			$this->success("删除成功！", U("Guestbookadmin/index"));
		}else{
			$this->error('删除失败！');
		}
	}
	public function status(){
		$db = M('userinfo');
		$id=I("get.id",0,'intval');
		$data['status']= 1;
		$data['open_time']= time();
		$result=$db->where(array("id"=>$id))->save($data);
		if($result!==false){
			$this->success("已查看！", U("Guestbookadmin/index"));
		}else{
			$this->error('查看失败！');
		}
	}


}